package com.yellowpage.bigdata.action;

import java.io.UnsupportedEncodingException;
import java.util.*;

import com.yellowpage.bigdata.data.DefaultOption;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

import com.yellowpage.bigdata.data.NspOnofflogTaImeiMonthEntity;
import com.yellowpage.bigdata.service.NspOnofflogTaImeiMonthEntityService;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;

public class NspOnofflogTaImeiMonthEntityAction extends BaseAction {

    public String RESULT_QUERY = "query";

    private Configuration configuration;

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    private List<NspOnofflogTaImeiMonthEntity> nspOnofflogTaImeiMonthEntityList = new ArrayList<NspOnofflogTaImeiMonthEntity>();
    private List<Map> resultList = new ArrayList<Map>();

    private NspOnofflogTaImeiMonthEntity nspOnofflogTaImeiMonthEntity;
    private NspOnofflogTaImeiMonthEntityService nspOnofflogTaImeiMonthEntityService;

    private String selectedId;
    private String[] selectedIds;
    private String message;

    private String query_monthId;

    public void prepare() {
        super.prepare();

        if (this.selectedId != null && !this.selectedId.equals("")) {
            this.nspOnofflogTaImeiMonthEntity = nspOnofflogTaImeiMonthEntityService.getNspOnofflogTaImeiMonthEntity(Long.parseLong(selectedId));
        }
    }

    public String queryByPage() {
        initPageRender();

        Map queryMap = new HashMap();
        queryMap.put("monthId", query_monthId);
        resultList = nspOnofflogTaImeiMonthEntityService.queryNspOnofflogTaImeiMonthEntityByPage(queryMap, pageRender);

        setPageRenderToRequest();

        return RESULT_QUERY;
    }

    /**
     * ajax获取饼图
     */
    public String getPie() throws UnsupportedEncodingException {
        Map queryMap = new HashMap();
        queryMap.put("query_monthId", query_monthId);
        resultList = nspOnofflogTaImeiMonthEntityService.queryAllNspOnofflogTaImeiMonthEntityByMonth(queryMap);
        if (resultList != null && resultList.size() != 0) {
            //创建Option
            DefaultOption o = new DefaultOption();
            List legends = new ArrayList();
            Iterator<Map> iterator = resultList.iterator();
            while(iterator.hasNext()){
                Map map = iterator.next();
                legends.add(map.get("NAME"));
            }

            //设置所有统计纬度名称，可以设置多个 o.setLegends("新增","月新增")
            o.setLegends(legends.toArray());

            //设置图表标题和子标题
            o.setText("月新增用户分渠道统计TOP10", query_monthId == null ? "" : "统计日期:" + query_monthId);

            //设置显示名称和数据
            o.setPieData("新增数", resultList);

            //jackson返回
            HttpServletResponse response = ServletActionContext.getResponse();
            o.getPieOption(response);
        }

        return null;
    }


    /**
     * getters and setters
     *
     * @return
     */
    public void setNspOnofflogTaImeiMonthEntity(NspOnofflogTaImeiMonthEntity nspOnofflogTaImeiMonthEntity) {
        this.nspOnofflogTaImeiMonthEntity = nspOnofflogTaImeiMonthEntity;
    }

    public NspOnofflogTaImeiMonthEntity getNspOnofflogTaImeiMonthEntity() {
        return nspOnofflogTaImeiMonthEntity;
    }


    public void setNspOnofflogTaImeiMonthEntityService(NspOnofflogTaImeiMonthEntityService nspOnofflogTaImeiMonthEntityService) {
        this.nspOnofflogTaImeiMonthEntityService = nspOnofflogTaImeiMonthEntityService;
    }

    public List<NspOnofflogTaImeiMonthEntity> getNspOnofflogTaImeiMonthEntityList() {
        return nspOnofflogTaImeiMonthEntityList;
    }

    public void setNspOnofflogTaImeiMonthEntityList(List<NspOnofflogTaImeiMonthEntity> nspOnofflogTaImeiMonthEntityList) {
        this.nspOnofflogTaImeiMonthEntityList = nspOnofflogTaImeiMonthEntityList;
    }

    public String getSelectedId() {
        return selectedId;
    }

    public void setSelectedId(String selectedId) {
        this.selectedId = selectedId;
    }

    public String[] getSelectedIds() {
        return selectedIds;
    }

    public void setSelectedIds(String[] selectedIds) {
        this.selectedIds = selectedIds;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<Map> getResultList() {
        return resultList;
    }

    public void setResultList(List<Map> resultList) {
        this.resultList = resultList;
    }


    public String getQuery_monthId() {
        return this.query_monthId;
    }

    public void setQuery_monthId(String monthId) {
        this.query_monthId = monthId;
    }


}
